package com.alstudio.utils.formater.time;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;

import android.content.Context;

import com.alstudio.app.ALLocalEnv;
import com.loovee.imaohu.R;

/**
 * 格式化时间工具类
 * 
 * @author alonso lee
 * 
 */
public class ALTimeUtils {

	// 1毫秒
	public static int MILLI_SECOND = 1;
	// 1秒
	public static int SECOND = MILLI_SECOND * 1000;
	// 1分钟
	public static int MINUTE = 60 * SECOND;
	// 1小时
	public static int HOUR = 60 * MINUTE;

	/**
	 * 将毫秒时间格式为X星期X天X小时X分钟
	 * 
	 * @param ms
	 * @return
	 */
	public static String formatTime(long timeOfSeconds) {

		long cur = System.currentTimeMillis() / 1000;
		timeOfSeconds = cur - timeOfSeconds;

		long days = timeOfSeconds / (24 * 3600);

		// 1个星期前
		if (days >= 7) {
			return String.format(ALLocalEnv.getLocalEnv()
					.getApplicationContext().getString(R.string.TxtWeekAgo),
					days);
		}
		// 几天前
		if (days > 0) {
			return String.format(ALLocalEnv.getLocalEnv()
					.getApplicationContext().getString(R.string.TxtDaysAgo),
					days);
		}

		// 几小时前
		long hours = timeOfSeconds / 3600;
		if (hours > 0) {
			return String.format(ALLocalEnv.getLocalEnv()
					.getApplicationContext().getString(R.string.TxtHoursAgo),
					hours);
		}

		// 几分钟前
		long minutes = timeOfSeconds / 60;
		if (minutes > 0) {
			return String.format(ALLocalEnv.getLocalEnv()
					.getApplicationContext().getString(R.string.TxtMiniuteAgo),
					minutes);
		}

		// 1分钟前
		return String.format(ALLocalEnv.getLocalEnv().getApplicationContext()
				.getString(R.string.TxtMiniuteAgo), 1);

	}

	/**
	 * 格式化时间为00:00的格式
	 * 
	 * @param timeOfSeconds
	 * @return
	 */
	public static String formatTimeWithoutHour(long timeOfSeconds) {

		if (timeOfSeconds <= 0)
			return "00\':00\"";

		long minutes = (timeOfSeconds % 3600) / 60;
		long seconds = timeOfSeconds % 60;

		StringBuffer text = new StringBuffer();
		boolean isMinutesSeted = false;
		isMinutesSeted = true;

		{
			if (minutes < 10) {
				text.append("0" + minutes);
			} else {
				text.append(minutes);
			}

			text.append("':");

			if (isMinutesSeted) {
				if (seconds < 10) {
					text.append("0" + seconds);
				} else {
					text.append(seconds);
				}
			} else {
				text.append(seconds);
			}
			text.append("\"");

			return text.toString();
		}
	}

	/**
	 * 格式化时间为00:00:00的字符串
	 * 
	 * @param timeOfSeconds
	 * @return
	 */
	public static String formatTimeWithHour(long timeOfSeconds) {
		if (timeOfSeconds <= 0)
			return "00:00";

		long hours = timeOfSeconds / 3600;
		long minutes = (timeOfSeconds % 3600) / 60;
		long seconds = timeOfSeconds % 60;

		StringBuffer text = new StringBuffer();

		if (hours > 0) {
			if (hours < 10)
				text.append("0" + hours);
			else
				text.append(hours);

			text.append(":");
		}

		if (minutes < 10)
			text.append("0" + minutes);
		else
			text.append(minutes);

		text.append(":");

		if (seconds < 10)
			text.append("0" + seconds);
		else
			text.append(seconds);

		return text.toString();
	}

	/**
	 * 格式化倒计时时间
	 * 
	 * @param timeOfSeconds
	 * @return
	 */
	public static String formatTimeForCountDown(long timeOfSeconds) {

		if (timeOfSeconds <= 0)
			return "0\"";

		// long hours = timeOfSeconds / 3600;
		long minutes = (timeOfSeconds % 3600) / 60;
		long seconds = timeOfSeconds % 60;

		StringBuffer text = new StringBuffer();
		boolean isMinutesSeted = false;

		if (minutes >= 1) {
			text.append(minutes);
			text.append("'");
			isMinutesSeted = true;
		}

		if (isMinutesSeted) {
			if (seconds < 10) {
				text.append("0" + seconds);
			} else {
				text.append(seconds);
			}
		} else {
			text.append(seconds);
		}
		text.append("\"");

		return text.toString();

	}

	/**
	 * 格式化时间为X分X秒的字符串
	 * 
	 * @param timeOfSeconds
	 * @return
	 */
	public static String formatVoipCallingTime(Context context,
			long timeOfSeconds) {
		if (timeOfSeconds <= 0)
			return "0" + context.getString(R.string.TxtSecond);

		// long hours = timeOfSeconds / 3600;
		long minutes = (timeOfSeconds % 3600) / 60;
		long seconds = timeOfSeconds % 60;

		StringBuffer text = new StringBuffer();

		if (minutes > 0) {
			text.append(minutes);
			text.append(context.getString(R.string.TxtMinute));
		}

		if (seconds >= 0) {
			text.append(seconds);
			text.append(context.getString(R.string.TxtSecond));
		}

		return text.toString();
	}

	/**
	 * 格式化voip电话开始时间
	 * 
	 * @param millsecond
	 * @return
	 */
	public static String formartVoipStartTime(Context context, long millsecond) {
		SimpleDateFormat format = null;

		long bakMillSecond = millsecond;
		long cur = System.currentTimeMillis() / 1000;
		millsecond /= 1000;
		millsecond = cur - millsecond;

		long days = millsecond / (24 * 3600);
		days = Math.abs(days);

		// 昨天之前
		if (days > 1) {
			format = new SimpleDateFormat("yy-MM-dd");
			return format.format(new Date(bakMillSecond));
		}
		// 昨天
		if (days == 1) {
			return context.getString(R.string.TxtYesterday);
		}

		format = new SimpleDateFormat("HH:mm");
		return format.format(new Date(bakMillSecond));

	}

	// 获取时间列表
	public static String[] getDateYears() {

		List<Integer> list = new ArrayList<Integer>();

		DateFormat df1 = new SimpleDateFormat("yyyy/MM/dd");
		Date nowdate = new Date(System.currentTimeMillis());

		String nowstring = df1.format(nowdate);

		String[] curYear = nowstring.split("/");

		String yearstr = curYear[0];

		if (null != yearstr && !"".equals(yearstr)) {

			int intyear = Integer.parseInt(yearstr);

			for (int i = intyear - 100; i <= intyear - 17; i++) {

				list.add(i);
			}

		}
		String[] strs = new String[list.size()];

		for (int i = 0; i < list.size(); i++) {
			String year = (list.get(i)) + "";

			strs[i] = year;

		}

		list.clear();
		list = null;
		return strs;
	}

	/**
	 * 获取当前时间 月-日 时分秒
	 * 
	 * @return
	 */
	public static String getcurrentTime() {
		String time = "";

		try {
			DateFormat df1 = new SimpleDateFormat("MM/dd hh:mm:ss");
			Date now = new Date(System.currentTimeMillis());

			time = df1.format(now);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}

		return time;
	}

	/**
	 * 显示时间距离现在隔了1天以上的，显示格式为：
	 * 
	 * @param showtime
	 *            毫秒数
	 * @return
	 */
	public static String getChatShowtime(Date mDate) {
		DateFormat df1 = null;
		Date nowdate = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd", Locale.CHINA);
		String s = sdf.format(mDate);
		String s1 = sdf.format(nowdate);
		boolean flag = s.equals(s1);
		if (flag) {
			df1 = new SimpleDateFormat("HH:mm");
		} else {
			df1 = new SimpleDateFormat("MM月dd HH:mm");
		}
		return df1.format(mDate);
	}

	/**
	 * 获取时、分
	 * 
	 * @param time
	 * @return
	 */
	public static String formatHourMinute(long time) {
		// Date date = new Date(time);
		// SimpleDateFormat df1 = new SimpleDateFormat("HH:mm");
		return formateTime(time, "HH:mm");
	}

	public static String formatYear(long time) {
		return formateTime(time, "yyyy-MM-dd");
	}

	public static String formateTime(long time, String pattern) {
		Date date = new Date(time);
		SimpleDateFormat df1 = new SimpleDateFormat(pattern);
		return df1.format(date);
	}

	/**
	 * 判断是否为同一天
	 * 
	 * @param day1
	 * @param day2
	 * @return
	 */
	public static boolean isTheSameDay(long day1, long day2) {
		Date dayi = new Date(day1);
		Date dayj = new Date(day2);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd", Locale.CHINA);
		String s = sdf.format(dayi);
		String s1 = sdf.format(dayj);
		boolean flag = s.equals(s1);
		return flag;
	}

	public static String getNoticeTime(Context context, Date date) {

		String tody = context.getString(R.string.TxtToday);
		String yesterday = context.getString(R.string.TxtYesterday);
		String moreyesterday = context
				.getString(R.string.TxtTheDayBeforeYesterday);
		String nowtime = "";

		DateFormat df1 = new SimpleDateFormat("yyyy/MM/dd");
		DateFormat df2 = new SimpleDateFormat("HH:mm:ss");
		DateFormat df3 = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");

		Date nowdate = new Date(System.currentTimeMillis());

		String nowstring = df1.format(nowdate);
		String oldstring = df1.format(date);

		try {
			Date nowdate1 = df1.parse(nowstring);
			Date olddate2 = df1.parse(oldstring);

			long nowtimes = nowdate1.getTime();
			long oldtimes = olddate2.getTime();

			long c = (nowtimes - oldtimes) / (1000 * 60 * 60 * 24);

			if (c == 0) {

				nowtime = df2.format(date);
				nowtime = tody + nowtime;

			} else if (c == 1) {

				nowtime = df2.format(date);
				nowtime = yesterday + nowtime;

			} else if (c == 2) {

				nowtime = df2.format(date);
				nowtime = moreyesterday + nowtime;

			} else {

				nowtime = df3.format(date);
			}

		} catch (ParseException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}

		return nowtime;

	}

}
